/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bacoopc.gui2;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 *
 * @author Baco Sistemas
 */
public class ModeloParametros implements ListModel {

    public static class Parametro {
        public String nome;
        public double valor;

        public Parametro(String n, double v) {
            nome = n;
            valor = v;
        }
        
        @Override
        public String toString() {
            return nome + " : " + valor;
        }
    }

    List<Parametro> parametros = new ArrayList<Parametro>();

    Set<ListDataListener> listeners = new TreeSet<ListDataListener>();

    public void addListDataListener(ListDataListener l) {
        listeners.add(l);
    }

    public Object getElementAt(int index) {
        return parametros.get(index);
    }

    public int getSize() {
        return parametros.size();
    }

    public void removeListDataListener(ListDataListener l) {
        listeners.remove(l);
    }

    public void clear() {
        parametros.clear();
        for ( ListDataListener l : listeners )
                l.contentsChanged(new ListDataEvent(l,
                ListDataEvent.CONTENTS_CHANGED,
                parametros.size() - 1, parametros.size() - 1));
    }

    public void adicionar( Parametro p ) {
        parametros.add(p);
        for ( ListDataListener l : listeners )
                l.contentsChanged(new ListDataEvent(l,
                ListDataEvent.CONTENTS_CHANGED,
                parametros.size() - 1, parametros.size() - 1));
    }

}
